﻿package net.wangit.dev.wizard.element;

import java.util.List;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.dev.wizard.WizardUtil;
import net.wangit.jdbc.ColumnRow;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.Table;
import net.wangit.util.Checker;


public class ADeleteActionButton {

	public ADeleteActionButton() {
	}

	public static final void create(DB db, ActionContext ac, String domain,
			int unitType, String unitId, String tableName, String command,
			String forward, List<String> elements) throws Exception {
		String elementId = WizardUtil.getElementId("delete", elements);
		Table table = new Table("dev", "ELEMENT");
		ColumnRow cRow = table.addInsertRow();
		cRow.set("eid", elementId);
		cRow.set("uid", unitId);
		cRow.set("c", 41);
		cRow.set("l", "/common/images/tool/delete.png");
		cRow.set("n", LabelUtil.get(ac, "dev", "toolbar.delete"));
		cRow.set("b", 0);
		cRow.set("o", 6);
		cRow.set("cu", getOnclick(domain, unitId, elementId, unitType));
		cRow.set("a", "1");
		if (Checker.isEmpty(command))
			cRow.set("ac", (new StringBuilder(String.valueOf(tableName)))
					.append(",").append(unitId).toString());
		else
			cRow.set("ac", command);
		cRow.set("asf", forward);
		cRow.set("aff", forward);
		cRow.set("asm", LabelUtil.get(ac, "dev", "action.delete.1"));
		cRow.set("afm", LabelUtil.get(ac, "dev", "action.delete.0"));
		cRow.set("au", 1);
		cRow.set("aum", 9);
		cRow.set("s", 1);
		db.save(table);
	}

	private static final String getOnclick(String domain, String unitId,
			String elementId, int unitType) {
		String url = WizardUtil.getActionUrl(domain, unitId, elementId);
		StringBuffer att = new StringBuffer("onclick=");
		if (2 == unitType)
			att.append("doListDelete");
		else
			att.append("doFormDelete");
		att.append("('").append(url).append("','").append(unitId).append("')");
		return att.toString();
	}
}
